using System;
using System.Collections.Generic;
using System.Text;

using CMS.Data;
using CMS.Core.Domain;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;
using AjaxPro;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;

namespace CMS.Presenters.Presenters
{
    public class ListBaiVietPresenter
    {
        private IList<BaiViet> objList;
        private IListBaiVietView iObjView;
        private IBaiVietDao iObjDAO;
        
        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database. 
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        //Attaches this presenter to the view's events.
        public ListBaiVietPresenter(IListBaiVietView iObjView, IBaiVietDao iObjDAO)
        {
            if (iObjView == null) throw new ArgumentNullException("View may not be null");
            if (iObjDAO == null) throw new ArgumentNullException("LinhVucDao may not be null");
            this.iObjView = iObjView;
            this.iObjDAO = iObjDAO;            

            SubscribeViewToEvents();
        }

        public ListBaiVietPresenter()
        { 
        
        }


        void SubscribeViewToEvents()
        {
            iObjView.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
             //LoadViewFromModel();
        }

        public void LoadTheLoais(int maChucNang)
        {
            this.iObjView.TheLoaiList = this.iObjDAO.GetAllTheLoaiWithChucNang(maChucNang);           
        }    

        public void LoadViewFromModel(string maNSD, int maChucNang)
        {
            this.objList = this.iObjDAO.GetAllBaiVietWithChucNang(maChucNang);
            iObjView.BaiVietList = objList;
            // iObjView.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenCapNhatThongTinDanhMuc(maNSD);
        }

        //public void FilterBaiViets(int MaChucNang, int MaTheLoai, string TieuDe, string NguonTin,
        //                        string TrangThai, string StartDate, String EndDate)
        //{
        //    this.iObjView.BaiVietList = this.iObjDAO.FilterBaiViets(MaChucNang, MaTheLoai, TieuDe, NguonTin, TrangThai, StartDate, EndDate);
        //}

        #region Button Members
        // For Cancel button
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        // For Delete button
        public void ObjToDelete(int maBaiViet)
        {
            this.iObjDAO.Delete(iObjDAO.GetById(maBaiViet, false));            
        }

        // For Update button
        public void ObjToUpdate(int maBaiViet)
        {
            BaiViet obj = iObjDAO.GetById(maBaiViet, false);
            obj.TrangThai = !obj.TrangThai;
            this.iObjDAO.SaveOrUpdate(obj);
        }
        #endregion
    }
}
